import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.base import MIMEBase
from email import encoders
from email.mime.multipart import MIMEMultipart



class CAppEmail:
    """
    Reference: Python实现自动化发送邮件 https://www.jb51.net/article/234775.htm 
    """

    def __init__(self, server_host:str, server_port:int) -> None:
        self.server_host = server_host
        self.server_port = server_port
        
    def send_plain_text(self, from_addr, to_addr, auth_code, subject, content):

        # 创建邮件对象
        message = MIMEText(content, "plain", "utf-8")
        message["From"] = Header(from_addr, "utf-8")
        message["To"] = Header(to_addr, "utf-8")
        message["Subject"] = Header(subject, "utf-8")

        ret = True

        try:
            smtp_obj = smtplib.SMTP(self.server_host, self.server_port)    # 使用你的邮箱服务商的SMTP服务器地址和端口
            smtp_obj.login(from_addr, auth_code)  # 使用你的邮箱地址和密码登录
            smtp_obj.sendmail(from_addr, to_addr, message.as_string())
            print("邮件发送成功")
        except smtplib.SMTPException as e:
            print("邮件发送失败：", e)
            ret = False

        return ret

    def send_html_text(self, from_addr, to_addr, auth_code, subject, content):
        """
        """
        # 创建邮件对象
        message = MIMEText(content, "html", "utf-8")
        message["From"] = Header(from_addr, "utf-8")
        message["To"] = Header(to_addr, "utf-8")
        message["Subject"] = Header(subject, "utf-8")

        ret = True

        try:
            smtp_obj = smtplib.SMTP(self.server_host, self.server_port)    # 使用你的邮箱服务商的SMTP服务器地址和端口
            smtp_obj.login(from_addr, auth_code)  # 使用你的邮箱地址和密码登录
            smtp_obj.sendmail(from_addr, to_addr, message.as_string())
            print("邮件发送成功")
        except smtplib.SMTPException as e:
            print("邮件发送失败：", e)
            ret = False

        return ret

    def send_with_attachment(self, from_addr, to_addr, auth_code, subject, content):
        message = MIMEMultipart()
        message["From"] = Header(from_addr, "utf-8")
        message["To"] = Header(to_addr, "utf-8")
        message["Subject"] = Header(subject, "utf-8")

        basedir = "d:/gitee/python/src/"
        attachment = MIMEBase("application", "octet-stream")
        attachment.set_payload(open(basedir + "我的附件.txt", "rb").read())
        attachment.add_header("Content-Disposition", "attachment", filename=Header(basedir + "我的附件.txt", "utf8").encode())
        encoders.encode_base64(attachment)

        message.attach(attachment)

        ret = True

        try:
            smtp_obj = smtplib.SMTP(self.server_host, self.server_port)    # 使用你的邮箱服务商的SMTP服务器地址和端口
            smtp_obj.login(from_addr, auth_code)  # 使用你的邮箱地址和密码登录
            smtp_obj.sendmail(from_addr, to_addr, message.as_string())
            print("邮件发送成功")
        except smtplib.SMTPException as e:
            print("邮件发送失败：", e)
            ret = False

        return ret

if __name__ == "__main__":

    # 创建email 对象，设置邮箱地址和端口号
    ## 若为QQ邮箱，则服务端为smtp.qq.com，若为163邮箱，则服务端为smtp.163.com，其他邮箱大家可自行百
    ## 163邮箱的SMTP服务器地址是smtp.163.com。至于端口号，它根据是否启用SSL加密有所不同。
    ## 如果启用了SSL加密，那么端口号通常是465或994；如果未启用SSL加密，则端口号是25。
    objEmail = CAppEmail('smtp.163.com', 25)

    # objEmail.send_plain_text(from_addr="wqch19871217@163.com", 
    #                          to_addr="wqch19871217@163.com", 
    #                          auth_code="SACYAGLNRRXLOLBG",
    #                          subject = "邮件主题",
    #                          content = "纯文本邮件正文")
    
    # objEmail.send_html_text(from_addr="wqch19871217@163.com", 
    #                         to_addr="wqch19871217@163.com", 
    #                         auth_code="SACYAGLNRRXLOLBG",
    #                         subject = "邮件主题",
    #                         content = "<p>这是一封HTML文本的邮件</p>")
    
    objEmail.send_with_attachment(from_addr="wqch19871217@163.com", 
                            to_addr="wqch19871217@163.com", 
                            auth_code="SACYAGLNRRXLOLBG",
                            subject = "邮件主题",
                            content = "带附件邮件")